package value_compare.framework;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import java.util.Set;

/**
 * @author lihao
 * @since 2019-11-03 21:26:39
 */
public abstract class BaseCompare<L,R> extends AbstractComparable {

    protected L left;
    protected R right;

    protected final Logger log = LoggerFactory.getLogger(getClass());

    protected BaseCompare(L left, R right){
        this.left = left;
        this.right = right;
    }

    protected String getLeftString() {
        return left.toString();
    }

    protected String getRightString() {
        return right.toString();
    }

    protected abstract Set<CompareOperator> supported();

    /**
     * 进行比较操作
     * @param operator
     * @return
     */
    public boolean doCompare(CompareOperator operator) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        if(!supported().contains(operator)) {
            throw new RuntimeException("Not Support The Operator `".concat(operator.getSymbol()
                    .concat("`【").concat(operator.getDesc()).concat("】")));
        }
        Boolean ret = false;
        try {
            ret = (Boolean)operator.getComparableMethod().invoke(this);
        } catch (IllegalAccessException | InvocationTargetException e) {
            log.error("comparableMethod.invoke error...",e);
        }
        log.info("{} 执行`{}`操作：({} {} {}) = {}", this.getClass().getSimpleName(),
                operator.getDesc(), getLeftString(),
                operator.getSymbol(), getRightString(), ret);
        return ret;
    }

}
